package com.example.tfliteonwatch;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.nio.ByteBuffer;

import org.tensorflow.lite.Interpreter;


import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.util.HashMap;

import java.util.Map;

import java.nio.FloatBuffer;

public class training extends AppCompatActivity {

    private TextView losstext;
    private Button train;
    private Button next;
    private Interpreter interpreter;
    float loss[]=new float[1];
    void dotrain(){
        float var[][]=new float[1][6];
        for(int j=0;j<6;j++) {
            if(para.actionName[j].equals(para.action.get(para.setTimeIndex-1))){
                var[0][j]=1;
            }
        }
        int count=0;
        int i;
        while(testing.acclist.size()>count+500&&testing.grylist.size()>count+500){
            float[][][] data=new float[1][100][6];
            for(i=count;i<count+500;i+=5){
                //                        Log.d("","线程："+Thread.currentThread().getName()+" "+i+" "+acclist.size());
                //                        Log.d("","i:" +i+" "+(i-count)/5);
                testing.accdata a= testing.acclist.get(i);
                data[0][(i-count)/5][0]=a.x;
                data[0][(i-count)/5][1]=a.y;
                data[0][(i-count)/5][2]=a.z;
            }
            for(i=count;i<count+500;i+=5){
//                        Log.d("","i:" +i);
                testing.grydata a= testing.grylist.get(i);
                data[0][(i-count)/5][3]=a.x;
                data[0][(i-count)/5][4]=a.y;
                data[0][(i-count)/5][5]=a.z;

            }

            Map<String, Object> outputs = new HashMap<>();
            Map<String, Object> inputs = new HashMap<>();

            inputs.put("x", data);

            inputs.put("y", var);

            loss=new float[1];
            outputs.put("loss",FloatBuffer.wrap(loss));
            interpreter.runSignature(inputs, outputs, "train");
            Log.d("",loss[0]+"");

            count+=10;
        }
        losstext.setText("loss: "+loss[0]);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        interpreter= TFLiteLoader.newInstance(getApplicationContext()).get("trainable_model.tflite");
        para.sensorID++;
        para.firstwrite=0;
        setContentView(R.layout.activity_training);
        losstext=(TextView) findViewById(R.id.loss) ;


        Log.d("",para.action.get(para.setTimeIndex-1));

//                Log.d("","zhelidaxiao"+acclist.size());

        train=(Button) findViewById(R.id.train);
        train.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dotrain();
                try {
                    // Conduct the training jobs.

                    // Export the trained weights as a checkpoint file.
                    File outputFile = new File(getFilesDir(), "checkpoint.ckpt");
                    Map<String, Object> input = new HashMap<>();
                    input.put("checkpoint_path", outputFile.getAbsolutePath());
                    Map<String, Object> output = new HashMap<>();
                    interpreter.runSignature(input, output, "save");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.d("", para.setTimeIndex + " " + para.action.size());
            }
        });
        next=(Button) findViewById(R.id.nextaction);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("",para.setTimeIndex+" "+para.action.size());
                testing.acclist.clear();
                testing.grylist.clear();
                if(para.setTimeIndex!=para.action.size()){   //动作未测量完全
                    Intent intent=new Intent(training.this, testing.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
                else {                           //动作测量完
                    Intent intent=new Intent(training.this, testFinish.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });




    }
}